package com.servlet.login.root.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;


public class FileDownloadServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String fileName = req.getParameter("filename");
        String url = "/uploadfilrdir/" + fileName;
        System.out.println(url);

        resp.setContentType("application/octet-stream");
        fileName = URLEncoder.encode(fileName,"utf-8");
        resp.addHeader("Content-Disposition","attachment;filename="+fileName);

        System.out.println("执行到跳转前");
        //跳转页面
        req.getRequestDispatcher(url).forward(req, resp);
        System.out.println("执行到跳转后");
        resp.flushBuffer();
    }

}
